﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///YZXX 的摘要说明
/// </summary>
public partial class YZXX
{
    public enum typeYH { 普通用户 = 1, 教师, 机构 }
    public enum typeYZ { 手机 = 1, 邮箱 }
    public enum statusYZ { 验证中 = 1, 验证通过, 验证不通过 }
    public static YZXX GetByAddUserID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YZXX.FirstOrDefault(row => row.AddUserID == id);
        }
    }
    public static YZXX GetTrueByID_YZM(int id, string YZM)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YZXX.FirstOrDefault(row => row.ID == id && row.YZM == YZM && row.EndTime >= System.DateTime.Now);
        }
    }

    public static List<YZXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YZXX.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.YZXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<YZXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.YZXX.FirstOrDefault(row => row.AddUserID == id);
            if (objItem == null)
                return;
            context.YZXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<YZXX> GetAllByConditon(int userID, string EndTimeForm, string EndTimeTo, int typeYH, int typeYZ, int statusYZ, string desYZ)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.YZXX
                        select t;
            if (userID != 0)
                query = query.Where(row => row.AddUserID == userID);
            if (!string.IsNullOrEmpty(EndTimeForm))
                query = query.Where(row => row.EndTime >= DongDong.Common.Misc.ConvertToDateTime(EndTimeForm));
            if (!string.IsNullOrEmpty(EndTimeTo))
                query = query.Where(row => row.EndTime <= DongDong.Common.Misc.ConvertToDateTime(EndTimeTo));
            if (typeYH != 0)
                query = query.Where(row => row.UserType == typeYH);
            if (typeYZ != 0)
                query = query.Where(row => row.Type == typeYZ);
            if (statusYZ != 0)
                query = query.Where(row => row.Status == statusYZ);
            if (!string.IsNullOrEmpty(desYZ))
                query = query.Where(row => row.YZDX.Contains(desYZ));
            query = query.OrderByDescending(row => row.EndTime);
            return query.ToList();
        }
    }
}